// 新增资讯
import React from 'react'
import {Row, Col, Button, message} from 'antd'
import styles from './style.css'
import AddForm from './addForm'
import { connect } from 'dva'

class AddArticle extends React.Component{
    constructor(props) {
        super(props);
        this.state={
            pic:'',
            tagList:[]
        }
    }
    //保存
    saveForm =()=> {
        const {tagList} = this.state;
        this.form.validateFields((err,values)=>{
            if(err){
                return;
            }
            if(tagList.length > 0){
                values.tagList = tagList;
                this.props.dispatch({
                    type:'article/addArticle',
                    payload:values
                });
            }else{
                message.error('关键字为必选项！',3);
            }
            
        })
    }
    //获取表单dom
    addForm =(form)=>{
        this.form = form;
    }
    //获取选择的关键字
    handleTags = (nextSelectedTags)=>{
        this.setState({
            tagList: nextSelectedTags
        })
    }
    render(){
        return (
            <div className={styles.main}>
                <Row>
                    <Col span={12} className={styles.col}>
                        <span className={styles.icon}>新增资讯</span>
                    </Col>
                    <Col span={12} className={styles.col}>
                        <Button className={styles.Button} type="primary" onClick={this.saveForm}>保存</Button>
                    </Col>
                </Row>
                <Row className={styles.model}></Row>
                <AddForm 
                    ref={this.addForm}
                    handlePic={this.handlePic}
                    handleTags={this.handleTags}
                />
            </div>
        )
    }
}
export default connect(({article})=>({article}))(AddArticle);